/**
 * 
 */
package com.agilegroup.crm.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.MenuItem;
import com.agilegroup.crm.AgileApplication;
import com.agilegroup.crm.adapters.ViewDetailsAdapter;
import com.agilegroup.crm.model.Account;
import com.agilegroup.crm.model.EntryItem;
import com.agilegroup.crm.model.Item;
import com.agilegroup.crm.model.SectionItem;
import com.agilegroup.crm.requests.ViewAccountJsonRequest;
import com.octo.android.robospice.persistence.DurationInMillis;
import com.octo.android.robospice.persistence.exception.SpiceException;
import com.octo.android.robospice.request.listener.RequestListener;

/**
 * @author Hisham
 *
 */
public class ViewAccountActivity extends SherlockListActivity {
    /** Called when the activity is first created. */
	private ArrayList<Item> items = new ArrayList<Item>();
	private static final String JSON_CACHE_KEY = "view_account_json";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        String accountId = intent.getStringExtra("accountId");
        AgileApplication.getInstance().spiceManager.execute(new ViewAccountJsonRequest(accountId),JSON_CACHE_KEY,DurationInMillis.ALWAYS_EXPIRED,new ViewAccountJsonRequestListener());
    }
    
    public class ViewAccountJsonRequestListener implements RequestListener<Account>{

		public void onRequestFailure(SpiceException spiceException) {
			Toast.makeText(ViewAccountActivity.this, "Failure", Toast.LENGTH_SHORT).show();
		}
		public void onRequestSuccess(Account account) {
			items.add(new SectionItem("Required Information"));
			if(account.getAccountCreatorName()!=null&&!account.getAccountCreatorName().equalsIgnoreCase("")){
				items.add(new EntryItem("Account Owner", account.getAccountCreatorName()));
			}
			else{
				items.add(new EntryItem("Account Owner", "  "));
			}
			if(account.getAccountName()!=null&&!account.getAccountName().equalsIgnoreCase("")){
				items.add(new EntryItem("Account Name", account.getAccountName()));
			}
			else{
				items.add(new EntryItem("Account Name", "  "));
			}
			
			
			items.add(new SectionItem("Account Information"));
			if(account.getCompanyName()!=null&&!account.getCompanyName().equalsIgnoreCase("")){
				 items.add(new EntryItem("Company", account.getCompanyName()));
			}
			else{
				items.add(new EntryItem("Company", "  "));
			}
	        if(account.getPhone()!=null&&!account.getPhone().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Phone", account.getPhone()));
	        }
	        else{
				items.add(new EntryItem("Phone", "  "));
			}
	        if(account.getMobile()!=null&&!account.getMobile().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Mobile", account.getMobile()));
	        }
	        else{
				items.add(new EntryItem("Mobile", "  "));
			}
	        if(account.getFax()!=null&&!account.getFax().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Fax", account.getFax()));
	        }
	        else{
				items.add(new EntryItem("Fax", "  "));
			}
	        if(account.getEmail()!=null&&!account.getEmail().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Email", account.getEmail()));
	        }
	        else{
				items.add(new EntryItem("Email", "  "));
			}
	        	
	        items.add(new SectionItem("Address Information"));
	        if(account.getStreet()!=null&&!account.getStreet().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Street", account.getStreet()));
	        }
	        else{
				items.add(new EntryItem("Street", "  "));
			}
	        if(account.getCityName()!=null&&!account.getCityName().equalsIgnoreCase("")){
	        	items.add(new EntryItem("City", account.getCityName()));
	        }
	        else{
				items.add(new EntryItem("City", "  "));
			}
	        if(account.getCountryName()!=null&&!account.getCountryName().equalsIgnoreCase("")){
	        	if(account.getCountryName().equals("--none--")){
	        		items.add(new EntryItem("Country", "  "));
	        	}
	        	else {
	        		items.add(new EntryItem("Country", account.getCountryName()));
				}
	        }
	        else{
				items.add(new EntryItem("Country", "  "));
			}
	        if(account.getState()!=null&&!account.getState().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Area", account.getState()));
	        }
	        else{
				items.add(new EntryItem("Area", "  "));
			}
	        if(account.getZipCode()!=null&&!account.getZipCode().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Zip/Postal Code", account.getZipCode()));
	        }
	        else{
				items.add(new EntryItem("Zip/Postal Code", "  "));
			}

	        if(account.getWebSite()!=null&&!account.getWebSite().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Website", account.getWebSite()));
	        }
	        else{
				items.add(new EntryItem("Website", "  "));
			}
	        
	        items.add(new SectionItem("Additional Information"));
	        if(account.getEmpNum()!=null){
	        	items.add(new EntryItem("No. of Employees", account.getEmpNum().toString()));
	        }
	        else{
				items.add(new EntryItem("No. of Employees", "  "));
			}
	        if(account.getAnnualRevenue()!=null){
	        	items.add(new EntryItem("Annual Revenue", account.getAnnualRevenue().toString()));
	        }
	        else{
				items.add(new EntryItem("Annual Revenue", "  "));
			}
	        if(account.getDescription()!=null&&!account.getDescription().equalsIgnoreCase("")){
	        	items.add(new EntryItem("Description", account.getDescription()));
	        }
	        else{
				items.add(new EntryItem("Description", "  "));
			}
	        if(account.getType()!=null&&!account.getType().equalsIgnoreCase("")){
	        	if(account.getType().equals("--none--")){
	        		items.add(new EntryItem("Type", "  "));
	        	}
	        	else {
	        		items.add(new EntryItem("Type", account.getType()));
				}
	        }
	        else{
				items.add(new EntryItem("Type", "  "));
			}
	        if(account.getIndustry()!=null&&!account.getIndustry().equalsIgnoreCase("")){
	        	if(account.getIndustry().equals("--none--")){
	        		items.add(new EntryItem("Industry", "  "));
	        	}
	        	else {
	        		items.add(new EntryItem("Industry", account.getIndustry()));
				}
	        }
	        else{
				items.add(new EntryItem("Industry", "  "));
			}
	        ViewDetailsAdapter adapter = new ViewDetailsAdapter(ViewAccountActivity.this, items);
	        setListAdapter(adapter);
		}
	 }
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	int itemId = item.getItemId();
        switch (itemId) {
        case android.R.id.home:
             finish();
            break;

        }

        return true;
    }
}